﻿using Manhua2.Common;
using Manhua2.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Manhua2.Helper;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace Manhua2.Pages
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class DownloadPage : Manhua2.Controls.LayoutAwarePage
    {
        public DownloadPage()
        {           
            this.InitializeComponent();

            this.Loaded += DownloadPage_Loaded;
            this.Unloaded += DownloadPage_Unloaded;
        }

        void DownloadPage_Unloaded(object sender, RoutedEventArgs e)
        {
            DownloadManager.ItemCompleted -= DownloadManager_ItemCompleted;
        }

        void DownloadPage_Loaded(object sender, RoutedEventArgs e)
        {
            DownloadManager.ItemCompleted += DownloadManager_ItemCompleted;
        }

        private void DownloadManager_ItemCompleted(object sender, DownloadItem e)
        {
            DownloadManager.CurrentDownloadItems.Remove(e);
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.DefaultViewModel["Download"] = DownloadManager.CurrentDownloadItems;
        }

        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            this.gvDownloads.SelectionMode = ListViewSelectionMode.Multiple;

            if ((string)this.btnSelectAll.Tag == "全选")
            {
                this.gvDownloads.SelectAll();
                this.btnSelectAll.Tag = "取消全选";
            }
            else
            {
                this.gvDownloads.SelectedItems.Clear();
                this.btnSelectAll.Tag = "全选";
            }
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (var item in this.gvDownloads.SelectedItems)
                {
                    var download = (DownloadItem)item;
                    if (download.Status != "下载完成")
                    {
                        DownloadManager.PauseDownload(download);
                        download.Status = "已暂停";
                    }
                }
            }
            catch(Exception ex)
            {
                MethodHelper.ShowMessageDialog(ex.Message);
            }
        }



        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in this.gvDownloads.SelectedItems)
            {
                var download = (DownloadItem)item;

                if (download.Status != "下载完成")
                    download.CTS.Cancel();
                else
                    DownloadManager.CurrentDownloadItems.Remove(download);
            }
        }

        private void btnResume_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (var item in this.gvDownloads.SelectedItems)
                {
                    var download = (DownloadItem)item;
                    if (download.Status == "已暂停")
                    {
                        DownloadManager.ResumeDownload(download);
                        download.Status = "正在下载";
                    }
                }
            }
            catch (Exception ex)
            {
                MethodHelper.ShowMessageDialog(ex.Message);
            }
        }
    }
}
